package com.wangchao.administrator.zenmechi.ui.fragment.caipuitemfragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wangchao.administrator.zenmechi.R;
import com.wangchao.administrator.zenmechi.ui.stepactivity.StepActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/17.
 */
public class CaipuItemFragment extends Fragment implements CaipuItemContract.View {

    @BindView(R.id.lv_caipu)
    PullToRefreshListView lvCaipu;
    private ListView lv;
    private View view;
    private String id;
    private CaipuItemPresenter presenter;

    public CaipuItemFragment(String id) {
        this.id = id;
    }

    public CaipuItemFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_caipuitem, container, false);
        ButterKnife.bind(this, view);

        lv = lvCaipu.getRefreshableView();
        lvCaipu.setMode(PullToRefreshBase.Mode.BOTH);
        presenter = new CaipuItemPresenter(this);
        presenter.getDetail(getActivity());
        refreshPage();
        jumpActivity();
        return view;
    }

    private void jumpActivity() {
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String itemId = presenter.getItemId(i);
                Intent intent = new Intent(getActivity(), StepActivity.class);
                intent.putExtra("id",itemId);
                startActivity(intent);
            }
        });
    }

    private void refreshPage() {
        lvCaipu.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                presenter.PullDownRefresh(getActivity());
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                presenter.PullUpRefresh(getActivity());
            }
        });
    }

    @Override
    public String getIds() {
        return id;
    }

    @Override
    public void getDetail(MyAdapterForCaipuList adapter) {
        lv.setAdapter(adapter);
        lvCaipu.onRefreshComplete();
    }

}
